package com.example.ccj.b.c.custom;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;


/**
 * 自定义控件的的标题栏
 * 2022年9月16日16:56:19
 */
public class TitleBarOnDisplayControl extends LinearLayout {

    private TextView mTvTitle,mTvTag,mTvLeftTag;
    private MImageView mImgRight;
    private ProgressBar mProgress;
    private ShowDashLineView showDashLineView;

    private String titleName;
    private String tagName;
    private @DrawableRes int  rightImg = 0;

    public TitleBarOnDisplayControl(Context context) {
        super(context);
        init(context);
    }

    public TitleBarOnDisplayControl(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.TitleBarOnDisplayControl);
        titleName = t.getString(R.styleable.TitleBarOnDisplayControl_title_name);
        tagName = t.getString(R.styleable.TitleBarOnDisplayControl_tag_name);
        rightImg = t.getResourceId(R.styleable.TitleBarOnDisplayControl_right_img_src,R.mipmap.def_img_select);

        init(context);
        t.recycle();
    }

    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.custom_display_type_title, this);
        mTvTitle = findViewById(R.id.display_type_title_title);
        mTvTag = findViewById(R.id.diaplay_type_title_tip);
        mImgRight = findViewById(R.id.diaplay_type_title_right_img);
        mProgress = findViewById(R.id.diaplay_type_title_right_progress);
        showDashLineView = findViewById(R.id.diaplay_type_dash_line);
        mTvLeftTag = findViewById(R.id.display_type_title_left_color);


        if (StringUtils.isNoEmpty(titleName)) mTvTitle.setText(titleName);
        if (StringUtils.isNoEmpty(tagName)) mTvTag.setText(tagName);
        if (rightImg != 0) mImgRight.setImageResource(rightImg);
    }

    public void setTitle(String title){
        titleName = title;
        mTvTitle.setText(title);
    }

    public void setTag(String tag){
        tagName = tag;
        mTvTag.setText(tag);
    }

    public void setImageOnclick(OnClickListener listener){
        this.mImgRight.setVisibility(VISIBLE);
        this.mImgRight.setOnClickListener(listener);
    }

    /**
     * 设置右侧图片及点击事件
     * @param imageResource 图片资源
     * @param listener 点击事件
     */
    public void setImageOnclick(@DrawableRes int imageResource, OnClickListener listener){
        rightImg = imageResource;
        this.mImgRight.setImageResource(imageResource);
        this.mImgRight.setVisibility(VISIBLE);
        this.mImgRight.setOnClickListener(listener);
    }

    public void goneAddImage(){
        this.mImgRight.setVisibility(GONE);
    }

    public TextView getTvTitleView() {
        return mTvTitle;
    }

    public TextView getTvTagView() {
        return mTvTag;
    }

    public MImageView getImgRightView() {
        return mImgRight;
    }

    public ProgressBar getProgressView() {
        return mProgress;
    }

    public void showDashLine(){
        showDashLineView.setVisibility(VISIBLE);
    }

    public void setLeftTagColor(int color) {
        this.mTvLeftTag.setBackgroundColor(color);
    }
    public TextView getTvLeftTag() {
        return mTvLeftTag;
    }
}

